﻿//*********************************************************************
//
//  文件名：BaseController
//
//  作者： LW
//
//  作用：
//
//  日期：
//
//*********************************************************************
using System;
using System.Linq;
using System.Web.Http;
using Framework.Common;
using System.Security.Claims;
using System.Threading;
using Application.WebAPI.Filter;
using System.Net.Http;
using YouTravel.DomainModels;
using YouTravel.Framework.Enums;
using System.Web;
using YouTravel.ViewModels;

namespace Application.WebAPI.Controllers
{
    /// <summary>
    /// BaseController
    /// 基础api
    /// 所有子api继承自该类
    /// 提供：
    ///     当前授权用户信息....
    /// </summary>
    //[AuthorizeFilter]
    public class BaseController : ApiController
    {
        //
        // GET: /Base/

        protected CommonGlobal commonGlobal = CommonGlobal.getInstance();

        protected string LoginUserName()
        {
            ClaimsPrincipal principal = Thread.CurrentPrincipal as ClaimsPrincipal;
            var userName = principal.Claims.Where(c => c.Type == "sub").Single().Value;
            return userName;
        }

        public UserViewModel LoginUser
        {
            get
            {
                return GetLoginUser();
            }
        }

        private UserViewModel GetLoginUser()
        {
            ClaimsPrincipal principal = Thread.CurrentPrincipal as ClaimsPrincipal;
            var userId = principal.Claims.Where(c => c.Type == "sub").Single().Value;
            var tmp = RedisHelp.GetLoginUserCacheNotNull(int.Parse(userId));
            return tmp;
        }


        /// <summary>
        /// 是否是超级管理员
        /// </summary>
        public bool IsSystemAdmin
        {
            get
            {
                if ((int)LoginUser.Type == 0)
                {
                    return true;
                }
                return false;
            }
        }

        /// <summary>
        /// 返回Json对象
        /// </summary>
        /// <param name="json"></param>
        /// <returns></returns>
        public HttpResponseMessage JsonObject(string json)
        {
            return new HttpResponseMessage { Content = new StringContent(json, System.Text.Encoding.UTF8, "application/json") };
        }
        
    }
}
